//
//  WmcEditPhotoVM.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/18.
//

import RxRelay
import UIKit
import YPBedrock

class WmcEditPhotoVM: WmcBaseVM {
    // Internal

    /// 传入的图片
    var inputImage: UIImage?

    /// 处理好的编辑图片
    var editImage: UIImage?

    /// 保存完成
    var saveSubject: PublishSubject<UIImage> = .init()

    /// 处理图片
    func disposeImage(_ image: UIImage, tagImage: UIImage, completeBlock: YPBlock.OneParam<UIImage>? = nil) {
        YPGCD.Async { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.editImage = weakSelf.ayncDisposeImage(image, tagImage: tagImage)
            YPGCD.Main {
                if let image = weakSelf.editImage {
                    completeBlock?(image)
                }
            }
        }
    }

    /// 异步线程处理图片
    @discardableResult
    func ayncDisposeImage(_ image: UIImage, tagImage: UIImage) -> UIImage? {
        // 设置原图画布
        guard image.size.width > 0 && image.size.height > 0 else {
            return nil
        }
        UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
        let imageDrawRect = CGRect(origin: .zero, size: image.size)
        image.draw(in: imageDrawRect)
        tagImage.draw(in: imageDrawRect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return newImage
    }

    func saveImage(_ image: UIImage) {
//        // 存入本地相册数据库
//        WmcLocalAlbumDBManager.insertImage(image)
//        // 存入系统相册
//        WmcAlbumManager.saveMyAlbumCollection(image) { [weak self] in
//            guard let weakSelf = self else { return }
//            // 通知回调
//            weakSelf.saveSubject.onNext(image)
//        }
        UIImageWriteToSavedPhotosAlbum(image, self, #selector(self.saveImage(image:didFinishSavingWihteError:contextInfo:)), nil)
    }

    /// 分享朋友圈
    func shareFirendCircle() {
        guard let image = editImage else { return }
        WmcShareManager.shareImage(image,
                                   title: nil,
                                   description: nil,
                                   messageExt: nil,
                                   scene: .friendCircle,
                                   completion: nil)
    }

    /// 分享微信
    func shareWechat() {
        guard let image = editImage else { return }
        WmcShareManager.shareImage(image,
                                   title: nil,
                                   description: nil,
                                   messageExt: nil,
                                   scene: .session,
                                   completion: nil)
    }

    // Private

    @objc
    private func saveImage(image _: UIImage, didFinishSavingWihteError error: Error?, contextInfo _: AnyObject) {
        guard let image = editImage else { return }
        if error != nil {
            WmcHudManager.toast("保存失败")
        } else {
            WmcHudManager.toast("保存成功")
            // 通知回调
            saveSubject.onNext(image)
        }
    }
}
